package org.pada.ext;

import org.pada.ext.annotation.IExtPt;

import java.util.function.Consumer;
import java.util.function.Function;

/**
 * @author pengshen
 * @date 2023.12.29 17:13
 */
public class ExtBus {

    public static <R, P extends IExt> R execute(Class<P> clazz, IExtPt extPt, Function<P, R> func) {
        final IExt ext = ExtHub.getExt(extPt);
        if (!clazz.isAssignableFrom(ext.getClass())) {
            throw new RuntimeException("can not find ext pt: " + extPt.getIdentity());
        }
        return func.apply((P) ext);
    }

    public static <P extends IExt> void executeVoid(Class<P> clazz, IExtPt extPt, Consumer<P> func) {
        final IExt ext = ExtHub.getExt(extPt);
        if (!clazz.isAssignableFrom(ext.getClass())) {
            throw new RuntimeException("can not find ext pt: " + extPt.getIdentity());
        }
        func.accept((P) ext);
    }
}
